package com.lihua.newideas.handler;

import com.github.pagehelper.PageInfo;
import com.lihua.newideas.pojo.Article;
import com.lihua.newideas.pojo.MyUser;
import com.lihua.newideas.service.ArticleService;
import com.lihua.newideas.service.CommentService;
import com.lihua.newideas.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
* 管理员界面拦截器
* */
@Component
public class AdminInterceptorHandler implements HandlerInterceptor {

    @Autowired
    private MyUserService userService;

    @Autowired
    private ArticleService articleService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.getParameter("id");
//        request.
//        commentService.queryCommentByArticleIdAndUserName()
        int userNum = userService.queryUserNum();
        int ycNum = articleService.queryArticleNumByArticleStatus(0); //原创
        int zzNum = articleService.queryArticleNumByArticleStatus(1); //转载
        //已发布
        PageInfo<Article> releasePageInfo = articleService.queryArticleByArticleStatus(0, 1, 8);
        //转载
        PageInfo<Article> draftPageInfo = articleService.queryArticleByArticleStatus(1, 1, 8);

        request.setAttribute("userNum",userNum);
        request.setAttribute("ycNum",ycNum);
        request.setAttribute("zzNum",zzNum);
        request.setAttribute("releasePageInfo",releasePageInfo);
        request.setAttribute("draftPageInfo",draftPageInfo);
//        System.out.println("用户数："+userNum+"原创:"+ycNum+"转载："+zzNum);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
